package
{
	import crl.encrypt.ext.PlayerGlobalDefine;
	import crl.help.AirFileDropApp;
	
	import flash.desktop.NativeApplication;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.InvokeEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * 
	 * @author crl
	 * 
	 */
	[SWF(frameRate="30",width="250",height="200")]
	dynamic public class EncryAir extends AirFileDropApp
	{
		
		protected static var debug:TextField;
		
		///[Embed(source="..\\bin-debug\\EncryptApp.swf",mimeType="application/octet-stream")]
		
		
		public function EncryAir(){
			
			this.addEventListener(Event.ADDED_TO_STAGE,addToStageHandler);
			
		}
		private function addToStageHandler(event:Event):void{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.$0);
			
			var loaderContext:LoaderContext=new LoaderContext(false,new ApplicationDomain());
			loaderContext.allowCodeImport=true;
			
			//loader.loadBytes(this.$0(this),loaderContext);
			
			createDebug();
			
			var nativeApplication:NativeApplication = NativeApplication.nativeApplication;
			nativeApplication.addEventListener(InvokeEvent.INVOKE, invokeHandle);
		}
		
		private function createDebug():void{
			debug=new TextField();
			var format:TextFormat=new TextFormat();
			format.size=12;
			format.font="tahoma";
			debug.defaultTextFormat=format;
			//debug.autoSize=TextFieldAutoSize.LEFT;
			debug.textColor=0x990000;
			debug.width=300;
			debug.height=300;
			debug.multiline=true;
			//debug.wordWrap=true;
			//debug.mouseEnabled=false;
			//debug.text="crl";
			
			this.addChild(debug);
			stage.addEventListener(Event.RESIZE,resizeHandle);
		}
		
		private function resizeHandle(event:Event):void{
			debug.width=stage.stageWidth;
			debug.height=stage.stageHeight;
		}
		
		public static function log(newText:String):void{
			debug.appendText(newText+"\n");
			debug.scrollV=debug.maxScrollV;
		}
		
		override protected function dragDropFiles(list:Array):void{
			var file:File=list[0];
			doSWF(file);
		}
		
		private function doSWF(file:File):void{
			if(file.extension.toLowerCase() !="swf"){
				debug.text="不支持非swf文件格式";
				return;
			}
			
			var bytes:ByteArray=new ByteArray();
			var stream:FileStream=new FileStream();
			stream.open(file,FileMode.READ);
			stream.readBytes(bytes);
			stream.close();
			
			
			//this.$0(bytes,file.name);
			new EncryptApp().start(bytes,file.name,log);
			//PlayerGlobalDefine.instance.write(bytes);
		}
		
		private var closeInterval:int;
		private function invokeHandle(event:InvokeEvent):void{
			var filePath:String=event.arguments[0];
			var file:File=new File(filePath);
			if(file.exists && file.extension =="swf"){
				doSWF(file);
				log("命令行调用:"+filePath);
			}
			
			clearTimeout(closeInterval);
			if(filePath !=""){
				closeInterval=setTimeout(stage.nativeWindow.close,500);
			}
			
		}
		
	}
}
import flash.events.Event;

class abcdefghijklmnopqrstuvwxyz{
	//[Embed(source="en_EncryptApp.swf",mimeType="application/octet-stream")]
	internal static const $:Class;
}
var $i:*;
EncryAir.prototype.$0=function(e:*=null,name:String=""):*{
	if(e is EncryAir){
		return new (abcdefghijklmnopqrstuvwxyz.$);
	}
	if($i){
		$i.start(e,name,EncryAir.log);
		return;
	}
	$i=e.target.content;
}